﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;


public partial class Course_AllCourseInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        MaintainScrollPositionOnPostBack = true;
        if (!IsPostBack)
        {
            FillCourseInGrid();
        }

       
    }
    #region CourseDataBase Calls
    private DataTable FetchAllCourseInfo()
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
        SqlConnection conn = new SqlConnection(connStr);
        SqlDataAdapter dAd = new SqlDataAdapter();
        SqlCommand dCmd = new SqlCommand();
        DataTable dTable = new DataTable();
        try
        {
            dCmd.CommandText = "SelectAllCourses";
            dCmd.CommandType = CommandType.StoredProcedure;
            dCmd.Connection = conn;
            dAd.SelectCommand = dCmd;
            // now open the connection
            conn.Open();
            dAd.Fill(dTable);
            conn.Close(); // close the connection

            return dTable;
        }
        catch (Exception e)
        {
            throw e;
        }

        finally
        {
            dCmd.Clone();
            dAd.Dispose();
            conn.Dispose();
        }
    }
    private DataTable SearchCourses(string courseid, string name)
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
        SqlConnection conn = new SqlConnection(connStr);
        SqlDataAdapter dAd = new SqlDataAdapter();
        SqlCommand dCmd = new SqlCommand();
        DataTable dTable = new DataTable();
        try
        {
            dCmd.CommandText = "CoursesSearch";
            dCmd.CommandType = CommandType.StoredProcedure;
            dCmd.Connection = conn;
            SqlParameter[] prms = new SqlParameter[2];
            prms[0] = new SqlParameter("@CourseId", SqlDbType.VarChar, 30);
            prms[0].Value = courseid;
            prms[1] = new SqlParameter("@Name", SqlDbType.VarChar, 100);
            prms[1].Value = name;
            dCmd.Parameters.AddRange(prms);
            dAd.SelectCommand = dCmd;
            // now open the connection
            conn.Open();
            dAd.Fill(dTable);
            conn.Close(); // close the connection

            return dTable;
        }
        catch (Exception e)
        {
            throw e;
        }

        finally
        {
            dCmd.Clone();
            dAd.Dispose();
            conn.Dispose();
        }
    }
    #endregion

    private void FillCourseInGrid()
    {
        try
        {
            DataTable dTable = FetchAllCourseInfo(); ;
            if (dTable.Rows.Count > 0)
            {
                CourseInfoGridView.DataSource = dTable;
                CourseInfoGridView.DataBind();
            }
            else
            {
                dTable.Rows.Add(dTable.NewRow());
                CourseInfoGridView.DataSource = dTable;
                CourseInfoGridView.DataBind();
                int TotalColumns = CourseInfoGridView.Rows[0].Cells.Count;
                CourseInfoGridView.Rows[0].Cells.Clear();
                CourseInfoGridView.Rows[0].Cells.Add(new TableCell());
                CourseInfoGridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                CourseInfoGridView.Rows[0].Cells[0].Text = "No Record Found";
                Master.MasterMessageBoxText = Master.MasterMessageBoxText + "Operation Completed Sucessfully";
            }
        }
        catch (Exception e)
        {
            Master.MasterMessageBoxText = Master.MasterMessageBoxText + "--ERROR: " + e.Message;
        }
    }

    protected void GridViewCourseInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

    }
    protected void GridViewCourseInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void GridViewCourseInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridViewCourseInfo_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void GridViewCourseInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    protected void CourseSearchButton_Click(object sender, EventArgs e)
    {
        string searchName = CourseNameTxtBx.Text;
        string searchCourseId = CourseCourseIdTxtBx.Text;
        try
        {
            DataTable dTable = SearchCourses(searchCourseId, searchName); 
            if (dTable.Rows.Count > 0)
            {
                CourseInfoGridView.DataSource = dTable;
                CourseInfoGridView.DataBind();
            }
            else
            {
                dTable.Rows.Add(dTable.NewRow());
                CourseInfoGridView.DataSource = dTable;
                CourseInfoGridView.DataBind();
                int TotalColumns = CourseInfoGridView.Rows[0].Cells.Count;
                CourseInfoGridView.Rows[0].Cells.Clear();
                CourseInfoGridView.Rows[0].Cells.Add(new TableCell());
                CourseInfoGridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                CourseInfoGridView.Rows[0].Cells[0].Text = "No Record Found";
                Master.MasterMessageBoxText = Master.MasterMessageBoxText + "Operation Completed Sucessfully";
            }
        }
        catch (Exception ex)
        {
            Master.MasterMessageBoxText = Master.MasterMessageBoxText + "--ERROR: " + ex.Message;
        }   
    }
}